# -*- coding: utf-8 -*-
import sys

from PyQt4 import QtCore, QtGui
from httpWidget import Ui_HttpWidget
import os

class httpWidget(QtGui.QWidget):
	def __init__(self, parent=None):
		self.isParametersValid=True
		self.currentPage = ''
		self.currentPageNumber = 0
		self.lastPageNumber=0
		self.lastDownloadResult = True
		
		
		super(httpWidget, self).__init__(parent)
		self.ui = Ui_HttpWidget()
		self.ui.setupUi(self)
		self.ui.url_2.setReadOnly(True)				

		QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("loadProgress (int)"), self.displayLoadProgress)
		QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("loadFinished (bool)"), self.processHtml)
		QtCore.QObject.connect(self.ui.reload_2, QtCore.SIGNAL("clicked()"), self.start)
		QtCore.QObject.connect(self.ui.url_3, QtCore.SIGNAL("textChanged (const QString&)"), self.calculateFinalPage)
		QtCore.QObject.connect(self.ui.spinBox, QtCore.SIGNAL("valueChanged (const QString&)"), self.onSpinBoxChange)
		QtCore.QMetaObject.connectSlotsByName(self)
	
	def calculateFinalPage(self, urlName):
		text = urlName + str(self.ui.spinBox.value())
		self.ui.url_2.setText(text)
	
	def onSpinBoxChange(self, value):
		self.calculateFinalPage(self.ui.url_3.text())
	
	def displayLoadProgress(self, progress):
		text = self.trUtf8("""Закачиваем страницу для конвертации:  Прогресс """) + str(progress) + "%"
		self.ui.label.setText("<font color='" + "blue" + "'>" + text + "</font>")
		label2text=self.trUtf8("Текущая страница: ")+self.currentPage
		self.ui.label_5.setText("<font color='" + "blue" + "'>" + label2text + "</font>")
		
	def start(self):
		self.isParametersValid=True
		
		if (self.ui.url_2.text()) == '':
			self.setLabelText("Заполните название страницы для скачки", "red")
			self.isParametersValid=False
		if (self.ui.url_4.text()) == '':
			self.setLabelText("Укажите файл для сохранения книги", "red")
			self.isParametersValid=False
			
		if self.isParametersValid:
			unicodeName=self.trUtf8(self.ui.url_4.text())
			bookfilename=str(unicodeName)
			if os.path.exists(bookfilename):
				os.remove(bookfilename)
							
			self.currentPageNumber=self.ui.spinBox.value()
			self.lastPageNumber=self.ui.spinBox_2.value()
			self.loadNextPage()
					
	def setLabelText(self, text, color):
		text = self.trUtf8(text)
		self.ui.label.setText("<font color='" + color + "'>" + text + "</font>")
		
	def loadNextPage(self):
		if self.currentPageNumber<=self.lastPageNumber:
			self.currentPage = self.ui.url_3.text()+ str(self.currentPageNumber)
			self.ui.webView.setUrl(QtCore.QUrl(self.currentPage))
		else:
			self.setLabelText("Конвертация всех страниц закончена", "blue")
	
	def processHtml(self, result):
		if result:
			self.setLabelText("Стартуем конвертацию страницы", "blue")
			text = self.ui.webView.page().mainFrame().toHtml()
			merger = HtmlMerger()
			text=unicode(text)
			text = merger.getTextFromHtml(text)
			
			filename=unicode(self.ui.url_4.text())		
			file = open(filename, 'a')
			file.write(text)
			file.close()
			self.setLabelText("Страница успешно конвертирована", "blue")
		else:
			self.setLabelText("Закачка страницы не удалась", "blue")
		self.lastDownloadResult = result
		self.currentPageNumber=self.currentPageNumber+1
		self.loadNextPage()
			
class HtmlMerger():    
    def getTextFromHtml(self, htmlText):
		from BeautifulSoup import BeautifulSoup
		html=htmlText.encode("utf-8")
		soup = BeautifulSoup(html)
		table = soup.find('table', id="onlineread_host")
		import re
		out = ''
		for row in table.findAll(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9', 'p']):
			out = out + self.clean(row) + "\n"
		return out
     	
  
    def clean(self, val):
        import re
        val = str(val)
        val = re.sub(r'<br.*?>', "\n", val) #remove annoying spans
        val = re.sub(r'<span class=.*?>*?</span>', '', val) #remove annoying spans        
        val = re.sub(r'<.*?>', '', val) #remove tags
        val = re.sub(r'&nbsp;', '', val) #remove defises
        val = re.sub(r'следующий лист &gt;&gt;', '', val) #remove defises
        val = re.sub(r'&lt;&lt; предыдущий лист', '', val) #remove defises
        
        return val

if __name__ == "__main__":
	app = QtGui.QApplication(sys.argv)
	myapp = httpWidget()
	myapp.show()
	sys.exit(app.exec_())
